package com.woshua.vptabmodel;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import com.woshua.vptabmodel.transformer.RotateTopTransformer;
import com.woshua.vptabmodel.transformer.RotateTransformer;
import com.woshua.vptabmodel.transformer.ScaleTransformer;

public class BannerActivity extends AppCompatActivity {

    private int[] colors = {
            Color.RED,
            Color.BLUE,
            Color.YELLOW,
            Color.GREEN
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banner);
        ViewPager vpContent = findViewById(R.id.vpContent);
        ViewPager vpRotateContent = findViewById(R.id.vpRotateContent);
        ViewPager vpRotateTopContent=findViewById(R.id.vpRotateTopContent);
        vpContent.setOffscreenPageLimit(3);
//        vpContent.setPageMargin(40);
        vpContent.setAdapter(new InnerPagerAdapter());
        vpContent.setPageTransformer(true, new ScaleTransformer());

        vpRotateContent.setOffscreenPageLimit(3);
        vpRotateContent.setPageMargin(40);
        vpRotateContent.setAdapter(new InnerPagerAdapter());
        vpRotateContent.setPageTransformer(true,new RotateTransformer());

        vpRotateTopContent.setOffscreenPageLimit(3);
        vpRotateTopContent.setPageMargin(40);
        vpRotateTopContent.setAdapter(new InnerPagerAdapter());
        vpRotateTopContent.setPageTransformer(true,new RotateTopTransformer());
    }

    class InnerPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return colors.length;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            View view = new View(container.getContext());
            view.setBackgroundColor(colors[position]);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
            return view == o;
        }
    }
}
